/*
 * @Author: tanjunyi@doctorwork.com
 * @Date: 2020-06-21 22:11:20
 * @LastEditors: tanjunyi@doctorwork.com
 * @LastEditTime: 2020-06-21 22:41:41
 * @Description:
 */

let has = {};
let queue = [];

// 这个队列是否正在等待更新
//更新队列
function flushSchedulerQueue() {
	queue.forEach(watcher => {
		watcher.run();
	})
}


export function queueWatcher(watcher) {
	const id = watcher.id;

	if (has[id] == null) {
		// 注册到队列中，并标记为已注册
		has[id] = true;
		queue.push(watcher);

		// 调用更新时，开始刷新
		nextTick(flushSchedulerQueue); //调用渲染watcher

	}
}
/**
 * 清空执行队列
 */
let pendding = false;
function flushCallbacksQueue() {
	callbacks.forEach(fn => fn())
	pendding = false;
}

/**
 * 异步更新
 */
let callbacks = [];
export function nextTick(fn) {
	callbacks.push(fn);
	if (!pendding) {
		setTimeout(() => {
			flushCallbacksQueue();
		}, 0)
		pendding = true;
	}
}